﻿using System;
using System.Windows.Threading;
using PairingMatrix.Common.Command;
using PairingMatrix.Model;

namespace PairingMatrix.ViewModel
{
    public class TimerViewModel : ViewModelBase, ITimer
    {
        private readonly CardWallModel _model;
        private TimeSpan _interval;
        private TimeSpan _timeSpan;
        private DispatcherTimer _timer;

        public TimerViewModel(CardWallModel model)
        {
            this._model = model;
            _model.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(_model_PropertyChanged);
            _interval = new TimeSpan(0, 0, 0, 0, 500);
            _timeSpan = new TimeSpan();
            _timer = new DispatcherTimer();
            _timer.Interval = _interval;
            _timer.Tick += timer_Tick;
            ShowSecond = true;
        }

        void _model_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if(e.PropertyName == "StandUpTime")
            {
                _timeSpan = _model.StandUpTime;
                Minutes = (int)_timeSpan.TotalMinutes;
            }
        }

        private int _minutes;
        public int Minutes
        {
            get { return _minutes; }
            set
            {
                if(_minutes == value)
                    return;
                _minutes = value;
                OnPropertyChanged("Minutes");
            }
        }

        private bool _showSecond;
        public bool ShowSecond
        {
            get { return _showSecond; }
            set
            {
                if (_showSecond == value)
                    return;
                _showSecond = value;
                OnPropertyChanged("ShowSecond");
            }
        }

        private RelayCommand _toggleTimerStartStopCommand;
        public RelayCommand ToggleTimerStartStopCommand
        {
            get
            {
                return _toggleTimerStartStopCommand ??
                       (_toggleTimerStartStopCommand = new RelayCommand(param =>
                                                                        {
                                                                            if(_timer.IsEnabled)
                                                                            {
                                                                                _timer.Stop();
                                                                                ShowSecond = true;
                                                                                _model.SetStandupTime(_timeSpan);
                                                                            }
                                                                            else
                                                                                _timer.Start();
                                                                        }));
            }
        }

        void timer_Tick(object sender, EventArgs e)
        {
            _timeSpan = _timeSpan.Add(_interval);
            Minutes = (int)_timeSpan.TotalMinutes;
            ShowSecond = !ShowSecond;
        }

    }
}